package com.fancyy.scm.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间与字符串互转工具
 *
 * @author huangyin
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {

    /**
     * 把日期转化成字符串(不带时分秒)
     *
     * @param date
     * @return
     */
    public static String toDateFormat(Date date) {
        StringBuffer myDate = new StringBuffer();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        myDate.append(cal.get(Calendar.YEAR))
                .append("-").append(String.format("%02d", cal.get(Calendar.MONTH) + 1))
                .append("-").append(String.format("%02d", cal.get(Calendar.DATE)));
        return myDate.toString();
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getToday() {
        return toDateFormat(new Date());
    }

    public static String getNow() {
        return toTimeFormat(new Date());
    }

    /**
     * 把日期转化成字符串(带时分秒)
     *
     * @param date
     * @return
     */
    public static String toTimeFormat(Date date) {
        StringBuffer myDate = new StringBuffer();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        myDate.append(cal.get(Calendar.YEAR))
                .append("-").append(String.format("%02d", cal.get(Calendar.MONTH) + 1))
                .append("-").append(String.format("%02d", cal.get(Calendar.DATE)))
                .append(" ").append(String.format("%02d", cal.get(Calendar.HOUR_OF_DAY)))
                .append(":").append(String.format("%02d", cal.get(Calendar.MINUTE)))
                .append(":").append(String.format("%02d", cal.get(Calendar.SECOND)));
        return myDate.toString();
    }

    /**
     * 把日期字符串转换成Date(时分秒均为0)
     *
     * @param dateFormat 形如 [ 2012-02-03 ]
     * @return
     */
    public static Date toDate(String dateFormat) {
        try {
            if (dateFormat.contains("-")) {
                SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
                return (Date) myFmt.parse(dateFormat);
            } else {
                long time = Long.parseLong(dateFormat);
                return new Date(time);
            }
        } catch (ParseException e) {
            LogUtil.e("DataFormate Exception", e);
            return null;
        }
    }

    /**
     * 把日期字符串转换成Date
     *
     * @param dateFormat 形如 [ 2012-02-03 12:12:08 ]
     * @return
     */
    public static Date toTime(String dateFormat) {
        try {
            if (dateFormat.contains("-")) {
                // 规范格式
                dateFormat = dateFormat.replaceAll("/", "-").trim();
                String[] times = dateFormat.split("\\ ");
                if (times.length == 1) {
                    dateFormat = new StringBuffer(dateFormat).append(" 00:00:00").toString();
                } else {
                    times = dateFormat.split("\\:");
                    if (times.length == 1) {
                        dateFormat = new StringBuffer(dateFormat).append(":00:00").toString();
                    } else if (times.length == 2) {
                        dateFormat = new StringBuffer(dateFormat).append(":00").toString();
                    }
                }
                //
                SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return (Date) myFmt.parse(dateFormat);
            } else {
                long time = Long.parseLong(dateFormat);
                return new Date(time);
            }
        } catch (ParseException e) {
            LogUtil.e("DataFormate Exception", e);
            return null;
        }
    }

    public static int getThisYear() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.YEAR);
    }

    public static int getThisMonth() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.MONTH) + 1;
    }

    public static int getThisDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.DATE) + 1;
    }

    public static int getThisHour() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    public static int getThisMinute() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.MINUTE);
    }

    public static int getThisSecond() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.SECOND);
    }

    /**
     * 判断两个时间段时候存在交集
     *
     * @param beginTime1
     * @param endTime1
     * @param beginTime2
     * @param endTime2
     * @return true->有交集
     */
    public static boolean comparisonRQ(String beginTime1, String endTime1, String beginTime2, String endTime2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date b1 = df.parse(beginTime1);
            Date e1 = df.parse(endTime1);
            Date b2 = df.parse(beginTime2);
            Date e2 = df.parse(endTime2);
            if (b1.getTime() < b2.getTime() && b2.getTime() < e1.getTime()) {
                return true;
            }
            if (b1.getTime() < e2.getTime() && e2.getTime() < e1.getTime()) {
                return true;
            }
        } catch (Exception e) {
            LogUtil.e("DataFormate comparisonRQ Exception", e);
        }
        return false;
    }

    /**
     * 日期选择
     * @param activity
     * @param themeResId
     * @param tv
     * @param calendar
     */
    public static void showDatePickerDialog(Activity activity, final TextView tv, Calendar calendar) {
        // 直接创建一个DatePickerDialog对话框实例，并将它显示出来
        new DatePickerDialog(activity , new DatePickerDialog.OnDateSetListener() {
            // 绑定监听器(How the parent is notified that the date is set.)
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 此处得到选择的时间，可以进行你想要的操作
                tv.setText(year + "-" + String.format("%02d", monthOfYear+ 1 )
                        +  "-" + String.format("%02d",dayOfMonth));

            }
        }
                // 设置初始日期
                , calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

}